/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-5-10      ShiHao       first version
 */
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <errno.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <rtdef.h>

#include <aht10.h> //温湿度传感器
#include <as608.h> //指纹模块
#include <jiashiqi.h> //加湿器模块
#include <Lock.h>
#include <fengshan.h> //风扇模块
#include <lengning.h> //冷凝模块
#include <mensuo.h> //门锁模块
#include <LED.h> //灯光模块
//onenet模块
#include <onenet.h>
#include <paho_mqtt.h>
#include <send_all_control.h> //串口
//wifi模块
#include <wlan_mgnt.h>
#include <wlan_prot.h>
#include <wlan_cfg.h>
#include <spi_wifi_rw007.h>
#include <wifi2.h>
#define AHT_I2C_BUS_NAME "i2c3"

#define DBG_TAG "main"
#define DBG_LVL         DBG_LOG
#include <rtdbg.h>

/* 配置 LED 灯引脚 */
#define PIN_LED_B              GET_PIN(F, 11)      // PF11 :  LED_B        --> LED
#define PIN_LED_R              GET_PIN(F, 12)      // PF12 :  LED_R        --> LED
#define KEY_UP_NUM          GET_PIN(C,  5)
#define KEY_DOWN_NUM        GET_PIN(C,  1)
#define KEY_LEFT_NUM          GET_PIN(C,  0)
#define KEY_RIGHT_NUM        GET_PIN(C,  4)
#define WLAN_SSID "test_ssid"
#define WLAN_PASSWORD "12345678"
#define NET_READY_TIME_OUT (rt_tick_from_millisecond(15 * 1000))
rt_thread_t th1, th2, th3, th4, th5;
float Temp, Humi;

int main(void)
{

    device_control_init();

    adc_init();
    FAN_Init();
    MenSuo_Init();
    LENGNING_Init();
    LED_Init();
    JSQ_Init();
    onenet_mqtt_init();

    aht10_device_t AHT = aht10_init(AHT_I2C_BUS_NAME);
    rt_pin_mode(KEY_UP_NUM, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(KEY_DOWN_NUM, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(KEY_LEFT_NUM, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(KEY_RIGHT_NUM, PIN_MODE_INPUT_PULLUP);

    th1 = rt_thread_create("control", control_thread_entry, NULL, 2048, 10, 20);
    th2 = rt_thread_create("ahti2c", ahti2c_thread_entry, NULL, 2048, 9, 5);
    th3 = rt_thread_create("send", senddata_thread_entry, NULL, 2048, 9, 5);
    th4 = rt_thread_create("wifi", wifi_thread_entry, NULL, 2048, 8, 15);
//    th5 = rt_thread_create("onenet", onenet_thread_entry, NULL, 2048, 9, 10);

    if(th1 == RT_NULL || th2 == RT_NULL || th3 == RT_NULL || th4 == RT_NULL  || th5 == RT_NULL)
    {
        LOG_E("create failed\n");
        return -ENOMEM;
    }


    rt_thread_startup(th1);
    rt_thread_startup(th2);
    rt_thread_startup(th3);
    rt_thread_startup(th4);
//    rt_thread_startup(th5);
//    ALL_Init();


//     自动锁定定时器
    unlock_timer = rt_timer_create("auto_lock", timer_callback,
                                 NULL, 5000, RT_TIMER_FLAG_ONE_SHOT);

//     创建处理线程
    rt_thread_t tid = rt_thread_create("fp_lock", mensuo_thread_entry,
                                      NULL, 2048, 9, 10);
    rt_thread_startup(tid);
}
